package protocol;

import java.util.Iterator;

import org.json.simple.JSONObject;

public class HTTPResponseHeaders extends HTTPPart {
	private String fHTTPVersion;
	private String fStatus;
	private Cookies fCoockies;

	public Cookies getfCoockies() {
		return fCoockies;
	}

	public HTTPResponseHeaders(String statusCode, String HTTPVesrion,
			JSONObject headers) {
		super();
		fCoockies = new Cookies();
		this.fHTTPVersion = HTTPVesrion;
		fStatus = statusCode;
		if (headers != null) {
			this.fHeaders = headers;
		}
	}

	public String getfStatus() {
		return fStatus;
	}

	public void setfStatus(String fStatus) {
		this.fStatus = fStatus;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void updateHeaders(JSONObject headers) {
		for (Iterator iterator = headers.keySet().iterator(); iterator
				.hasNext();) {
			String key = (String) iterator.next();
			this.fHeaders.put(key, headers.get(key));
		}
	}

	@SuppressWarnings("unchecked")
	public void updateHeaders(String name, String value) {
		this.fHeaders.put(name, value);
	}

	@SuppressWarnings("rawtypes")
	public String toString() {
		String ret = new String();
		ret += this.fHTTPVersion + " " + this.fStatus + "\n";
		for (Iterator iterator = this.fHeaders.keySet().iterator(); iterator
				.hasNext();) {
			String key = (String) iterator.next();
			ret += key + ": " + this.fHeaders.get(key) + "\n";

		}
		if (this.fCoockies.toString().length() > 2) {
			ret += Headers.SET_COOKIE + ": " + this.fCoockies.toString()+"\n";
		}
		ret += "\n";
		return ret;
	}

	@Override
	public boolean isValid() {
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject toJSONObject() {
		JSONObject ret = new JSONObject();
		ret.put(HTTP.HTTP_VERSION, this.fHTTPVersion);
		ret.put(HTTP.HEADERS, this.fHeaders);
		ret.put(HTTP.HTTP_STATUS, this.fStatus);
		ret.put(Headers.COOKIE, this.fCoockies.toString());
		return ret;
	}

}
